package gameObject;

import main.Core;
import action.WaitAction;
import data.ImageSet;
import data.ImageSprite;

public class Char extends ImageSprite {

	public enum Facing { Down, Left, Right, Up };
	ImageSet spriteSet;
	WaitAction wait;
	Facing facing;
	float refreshTime;
	int currentFrame;
	boolean autoRefresh;
	boolean descending = false;

	int[] steps;
	
	public Char(ImageSet imgSet, int x, int y, int w, int h, Core c) {
		super(imgSet.getTile(0, 0), x, y, w, h, c);
		
		this.spriteSet = imgSet;
		this.wait = new WaitAction();
		this.facing = Facing.Up;
		this.refreshTime = 0.1f;
		this.currentFrame = 0;
		this.autoRefresh = true;
		
		this.wait.start(refreshTime);
	}
	
	@Override
	public void Update(float elapsedTime) {
		if (wait.link(false, elapsedTime)) {
			System.out.println(facing.name());
			if (autoRefresh) {
				if (descending) { // essa merda ai faz q ele fique fazendo o seguinte: 0 1 2 1 repeat
					switch (currentFrame) {
						case (2):
							currentFrame = 1;
							break;
						case (1):
							currentFrame = 0;
							descending = false;
							break;
						}
					}
					else {
						switch (currentFrame) {
						case (0):
							currentFrame = 1;
							break;
						case (1):
							currentFrame = 2;
							descending = true;
							break;
						}
					}
				bufferedImage = spriteSet.getTile(currentFrame, facing.ordinal());
			}
		}
	}

	
	public void changeFacing (Facing face) {
		facing = face;
		bufferedImage = spriteSet.getTile(currentFrame, facing.ordinal());
	}

	public void forceRefresh()
	{
		currentFrame = (currentFrame + 1) % (int)spriteSet.getTileNumber().getF1();
		bufferedImage = spriteSet.getTile(currentFrame, facing.ordinal());
	}
	
	public boolean isFinished()
	{
		if (currentFrame == (int)spriteSet.getTileNumber().getF1() - 1 )
			return true;
		else
			return false;
	}

	public Facing getFacing() {
		return facing;
	}
	public boolean isAutoRefresh() {
		return autoRefresh;
	}
	public void setAutoRefresh(boolean autoRefresh) {
		this.autoRefresh = autoRefresh; 
		currentFrame = 1;
		bufferedImage = spriteSet.getTile(currentFrame, facing.ordinal());
	}
}
